<?php
class CategoryModel extends Model
{
    public function __construct()
    {
        $this->loadTable('category');
    }
    

    public function getCategories($parent_id,&$array)
    {
        if(is_null($array))
            $array = array();
        $db = DataProvider::getInstance();
        $sql = "SELECT * FROM category";
        if(is_null($parent_id))
        {
            $sql .= " WHERE parent_id is NULL";
        }
        else
        {
            $sql .= " WHERE parent_id = $parent_id";
        }
        $rs = $db->_query($sql);
        $size = $rs->getSize();
        if($size == 0)
            return;
        for($i = 0; $i < $size; $i++)
        {
            $tempRow = $rs->getRow($i);
            $array[] = $tempRow;
            $this->getCategories($tempRow->id,$array);
        }
        return $array;
    }

    public function getCategory($id)
    {
        $result = NULL;
        $condition = array('id' => $id);
        $result  = $this->tables['category']->get($condition);
        return $result;
    }
}